<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>编辑物业费记录-物业费交款</title>
  </head>
  <body>
    <script type="text/javascript">
      Ext.onReady(function(){
        Ext.QuickTips.init();
        Ext.BLANK_IMAGE_URL = '/javascripts/ext/resources/images/default/s.gif';
        //设置对话框最大宽度
        Ext.Msg.getDialog().setWidth(300);

        var propertyLastYear ;
        var propertyLastMonth ;
        var unitPrice ;
        var houseId = new Ext.form.Hidden({
          name: 'house_id'
        });

        var propertyId = new Ext.form.Hidden({
          name: 'id'
        });

        var ownerNumber = new Ext.form.TextField({
          fieldLabel: '业主编号',
          allowBlank: false,
          name: 'number',
          anchor: '90%',
          readOnly:true
        });
        var owner = new Ext.form.TextField({
          fieldLabel: '业主姓名',
          allowBlank: false,
          name: 'owner',
          anchor: '90%',
          readOnly:true
        });
        
        var propertyLastDate = new Ext.form.TextField({
          fieldLabel: '交费结止',
          allowBlank: false,
          name: 'owner',
          anchor: '90%',
          readOnly:true
        });

        var area =  new Ext.form.TextField({
          fieldLabel: '面积',
          allowBlank: false,
          readOnly: true,
          name: 'area',
          anchor: '90%'
        });

        //本次金额
        var propertyAmount =  new Ext.form.NumberField({
          fieldLabel: '本次金额',
          allowBlank: false,
          name: 'amount',
          anchor: '90%',
          readOnly:true
        });

        var propertyCurrentYear = new Ext.form.ComboBox({ //combobox
          name: 'current_year',
          fieldLabel: '本次交费年',
          width: '90',
          resiable: true,
          typeAhead: true,
          allowBlank: false,
          handleHeight: 100, //
          displayField: 'key',
          valueField: 'value',
          mode: 'local',
          selectOnFocus: true,
          triggerAction: 'all',//这个属性非常重要,可以在选择一个值以后,依然还显示其它的值.
          readOnly: true,
          emptyText: '选择年...',
          store: new Ext.data.Store({
            data: [['2004', '2004'],['2005', '2005'],['2006', '2006'],['2007', '2007'],['2008', '2008'], ['2009', '2009'], ['2010', '2010'], ['2011', '2011'], ['2012', '2012']],
            reader: new Ext.data.ArrayReader({
              id: 0
            }, [{
                name: 'value'
              }, {
                name: 'key'
              }])
          }), //数据储存到这个comboBox
          anchor: '90%'
        });

        propertyCurrentYear.setValue(new Date().clearTime(true).getFullYear());
        propertyCurrentYear.on('select',countPropertyFee);
        
        var propertyCurrentMonth = new Ext.form.ComboBox({ //combobox
          name: 'current_month',
          fieldLabel: '月',
          width: '90',
          resiable: true,
          typeAhead: true,
          allowBlank: false,
          handleHeight: 100, //
          displayField: 'key',
          valueField: 'value',
          mode: 'local',
          selectOnFocus: true,
          triggerAction: 'all',//这个属性非常重要,可以在选择一个值以后,依然还显示其它的值.
          readOnly: true,
          emptyText: '选择月...',
          store: new Ext.data.Store({
            data: [['1', '1'], ['2', '2'], ['3', '3'], ['4', '4'], ['5', '5'], ['6', '6'],['7', '7'], ['8', '8'], ['9', '9'], ['10', '10'], ['11', '11'], ['12', '12']],
            reader: new Ext.data.ArrayReader({
              id: 0
            }, [{
                name: 'value'
              }, {
                name: 'key'
              }])
          }), //数据储存到这个comboBox
          anchor: '90%'
        });
        
        propertyCurrentMonth.on('select',countPropertyFee);
        
        var propertyUnitPrice =  new Ext.form.NumberField({
          fieldLabel: '本次单价',
          allowBlank: false,
          name: 'unit_price',
          anchor: '90%'
        });       
        
        //提交按钮
        var editPropertyFeeButton = new Ext.Button({
          text: '提交',
          iconCls: 'icon-save',
          handler: function(){
            editPropertyFeeButton.setDisabled(true);
            if (editPropertyRecordFormPanel.form.isValid()) {
              if(propertyCurrentYear.getValue() < propertyLastYear){
                Ext.MessageBox.show({
                  title: '提示消息',
                  msg: '本次结止年份小于上次结止日期！有误，请核实!',
                  buttons: Ext.MessageBox.OK,
                  icon: Ext.MessageBox.INFO
                });
                editPropertyFeeButton.enable();
                return;
              }else if(propertyCurrentYear.getValue() == propertyLastYear){
                if(propertyCurrentMonth.getValue() <= propertyLastMonth){
                  Ext.MessageBox.show({
                    title: '提示消息',
                    msg: '本次结止月份小于上次结止日期！有误，请核实!',
                    buttons: Ext.MessageBox.OK,
                    icon: Ext.MessageBox.INFO
                  });
                  editPropertyFeeButton.enable();
                  return;
                }
              }

              editPropertyRecordFormPanel.form.submit({
                success: function(result, resp){
                  if (resp.result.info.indexOf("成功") >= 0) {
                    editPropertyRecordWin.destroy();
                  } else {
                    Ext.MessageBox.alert('消息', resp.result.info);
                    editPropertyFeeButton.enable();
                  }
                },
                failure: function(result, request){
                  editPropertyFeeButton.enable();
                  Ext.MessageBox.alert('消息', '服务器的通信失败!');
                }
              });
            }
            else {
              editPropertyFeeButton.enable();
              //Ext.MessageBox.alert('输入验证有错', '请输入必输选项');
            }
          }
        })
        
        var editPropertyRecordFormPanel = new Ext.form.FormPanel({
          //baseCls: 'x-plain',
          labelWidth: 65,
          width: '400',
          labelAlign: 'right',
          frame : true,
          url: '/fee/updatePropertyRecord',
          reader: new Ext.data.JsonReader({
            root: 'root'
          }, [{
              name: 'id'
            },{
              name: 'house_id',
              mapping: 'house.id'
            },{
              name: 'number',
              mapping: 'house.number'
            },{
              name: 'owner',
              mapping: 'house.owner'
            },{
              name: 'area',
              mapping: 'house.area'
            },{
              name: 'unit_price'
            },{
              name: 'building_type',
              mapping: 'house.sub_type'
            },{
              name:'amount'
            },{
              name:'action_date'
            },{
              name:'last_year'
            },{
              name:'last_month'
            },{
              name:'current_year'
            },{
              name:'current_month'
            },{
              name:'action_date'
            },{
              name:'created_at'
            }]),
          //defaultType: 'textfield',
          items: [{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [ownerNumber]
                }, {
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [owner]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [propertyLastDate]
                }, {
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [area]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [propertyCurrentYear]
                }, {
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [propertyCurrentMonth]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [propertyUnitPrice]
                }, {
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [propertyAmount]
                }]
            },houseId,propertyId],
          buttons: [editPropertyFeeButton,{
              text: '关闭',
              iconCls: 'exit',
              handler: function(){
                editPropertyRecordWin.destroy();
              }
            }]
        });
        editPropertyRecordFormPanel.render('edit_property_form');
        editPropertyRecordFormPanel.form.load({
          url: '/fee/getPropertyById?property_id=<%=@propertyId%>',
          waitMsg: '正在载入数据...',
          success: function(form, response){
            propertyLastYear = parseInt(response.result.data.last_year);
            propertyLastMonth = parseInt(response.result.data.last_month);
            propertyLastDate.setValue(propertyLastYear+"年"+propertyLastMonth+"月");
            unitPrice = propertyUnitPrice.getValue();
            //countPropertyFee();
            //alert(propertyCurrentMonth.getValue());
          },
          failure: function(form, response){
            Ext.MessageBox.alert('提示信息', '信息加载失败');
          }
        });
        function countPropertyFee(){
          //本次选择的年月
          var cYear = parseInt(propertyCurrentYear.getValue());
          var cMonth = parseInt(propertyCurrentMonth.getValue());
          //上次交费的年月
          var lYear = propertyLastYear;
          var lMonth = propertyLastMonth;
          var areaNumber = area.getValue();
          if(cYear > lYear){
            //propertyAmount.setValue(Math.round(months*unitPrice*10*areaNumber*100/100)/10);
            var months = (cYear-1-lYear)*12 + (12-lMonth) + cMonth;
            var amount = months*unitPrice*areaNumber;
            propertyAmount.setValue(amount.toFixed(2));
          }else if(cYear == lYear){
            if(cMonth>lMonth){
              //propertyAmount.setValue(Math.round((cMonth - lMonth)*unitPrice*10*areaNumber*100/100)/10);
              var amount = (cMonth - lMonth)*unitPrice*areaNumber;
              propertyAmount.setValue(amount.toFixed(2));
            }else if(cMonth == lMonth){
              propertyAmount.setValue(0);
            }
          }
        };
      })
    </script>
    <div id="edit_property_form"></div>
  </body>
</html>
